package com.ruoyi.caipiao.service.impl;

import java.util.*;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.caipiao.utils.Pl5NumUtil;
import com.ruoyi.common.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.caipiao.mapper.CaipiaoPl5MethodOneMapper;
import com.ruoyi.caipiao.domain.CaipiaoPl5MethodOne;
import com.ruoyi.caipiao.service.ICaipiaoPl5MethodOneService;

/**
 * 排列5方法预测Service业务层处理
 *
 * @author ruoyi
 * @date 2025-06-07
 */
@Slf4j
@Service
public class CaipiaoPl5MethodOneServiceImpl extends ServiceImpl<CaipiaoPl5MethodOneMapper, CaipiaoPl5MethodOne> implements ICaipiaoPl5MethodOneService {
    @Autowired
    private CaipiaoPl5MethodOneMapper caipiaoPl5MethodOneMapper;

    @Override
    public Object getNums(String tb, Long id) {
        return caipiaoPl5MethodOneMapper;
    }

    /**
     * 查询排列5方法预测
     *
     * @param id 排列5方法预测主键
     * @return 排列5方法预测
     */
    @Override
    public CaipiaoPl5MethodOne selectCaipiaoPl5MethodOneById(Long id) {
        return caipiaoPl5MethodOneMapper.selectCaipiaoPl5MethodOneById(id);
    }

    /**
     * 查询排列5方法预测列表
     *
     * @param caipiaoPl5MethodOne 排列5方法预测
     * @return 排列5方法预测
     */
    @Override
    public List<CaipiaoPl5MethodOne> selectCaipiaoPl5MethodOneList(CaipiaoPl5MethodOne caipiaoPl5MethodOne) {
        return caipiaoPl5MethodOneMapper.selectCaipiaoPl5MethodOneList(caipiaoPl5MethodOne);
    }

    /**
     * 新增排列5方法预测
     *
     * @param caipiaoPl5MethodOne 排列5方法预测
     * @return 结果
     */
    @Override
    public int insertCaipiaoPl5MethodOne(CaipiaoPl5MethodOne caipiaoPl5MethodOne) {
        caipiaoPl5MethodOne.setCreateTime(DateUtils.getNowDate());
        return caipiaoPl5MethodOneMapper.insertCaipiaoPl5MethodOne(caipiaoPl5MethodOne);
    }

    /**
     * 修改排列5方法预测
     *
     * @param caipiaoPl5MethodOne 排列5方法预测
     * @return 结果
     */
    @Override
    public int updateCaipiaoPl5MethodOne(CaipiaoPl5MethodOne caipiaoPl5MethodOne) {
        return caipiaoPl5MethodOneMapper.updateCaipiaoPl5MethodOne(caipiaoPl5MethodOne);
    }

    /**
     * 批量删除排列5方法预测
     *
     * @param ids 需要删除的排列5方法预测主键
     * @return 结果
     */
    @Override
    public int deleteCaipiaoPl5MethodOneByIds(Long[] ids) {
        return caipiaoPl5MethodOneMapper.deleteCaipiaoPl5MethodOneByIds(ids);
    }

    /**
     * 删除排列5方法预测信息
     *
     * @param id 排列5方法预测主键
     * @return 结果
     */
    @Override
    public int deleteCaipiaoPl5MethodOneById(Long id) {
        return caipiaoPl5MethodOneMapper.deleteCaipiaoPl5MethodOneById(id);
    }

    @Override
    public List<CaipiaoPl5MethodOne> checkCode(CaipiaoPl5MethodOne caipiaoPl5MethodOne) {

        List<CaipiaoPl5MethodOne> list = caipiaoPl5MethodOneMapper.listNoCheckNum(caipiaoPl5MethodOne);
        if (!list.isEmpty()) {
            for (CaipiaoPl5MethodOne one : list) {
                if (one.getKjNum() != null) {
                    String kjNum = one.getKjNum();
                    boolean zj = true;
                    boolean zjPl3 = true;
                    if (!one.getPosition1().contains(kjNum.substring(0, 1))) {
                        zj = false;
                        zjPl3 = false;
                    }
                    if (!one.getPosition2().contains(kjNum.substring(1, 2))) {
                        zj = false;
                        zjPl3 = false;
                    }
                    if (!one.getPosition3().contains(kjNum.substring(2, 3))) {
                        zj = false;
                        zjPl3 = false;
                    }
                    if (!one.getPosition4().contains(kjNum.substring(3, 4))) {
                        zj = false;
                    }
                    if (!one.getPosition5().contains(kjNum.substring(4, 5))) {
                        zj = false;
                    }
                    if (zj) {
                        one.setRightPl5(1);
                    } else {
                        one.setRightPl5(2);
                    }

                    if (zjPl3) {
                        one.setRightPl3(1);
                    } else {
                        one.setRightPl3(2);
                    }

//                    caipiaoPl5MethodOneMapper.updateCaipiaoPl5MethodOne(one);
                }
            }
        }
        return list;
    }

    @Override
    public void generateCode(CaipiaoPl5MethodOne caipiaoPl5MethodOne, ArrayList<CaipiaoPl5MethodOne> caipiaoPl5MethodOnes) {
        if (caipiaoPl5MethodOne.getQs() == null) {
            throw new RuntimeException("请选择预测的期数!");
        }

        List<String> code = new ArrayList<>();
        Set<String> nums = Pl5NumUtil.getNums(300);
        for (; ; ) {
            nums.retainAll(Pl5NumUtil.getNums(300));
            if (nums.size() == 1) {
                code.addAll(nums);

                if (code.size() == 100) {
                    break;
                }

                nums = Pl5NumUtil.getNums(300);
            } else if (nums.isEmpty()) {
                nums = Pl5NumUtil.getNums(300);
            }
        }
        log.info("方法4：{}，{}" , nums.size(), nums.toString());

        caipiaoPl5MethodOnes.add(getCaipiaoPl5MethodOne(caipiaoPl5MethodOne, code, 9));
        caipiaoPl5MethodOnes.add(getCaipiaoPl5MethodOne(caipiaoPl5MethodOne, code, 8));
        caipiaoPl5MethodOnes.add(getCaipiaoPl5MethodOne(caipiaoPl5MethodOne, code, 7));
        caipiaoPl5MethodOnes.add(getCaipiaoPl5MethodOne(caipiaoPl5MethodOne, code, 6));
        caipiaoPl5MethodOnes.add(getCaipiaoPl5MethodOne(caipiaoPl5MethodOne, code, 5));
        caipiaoPl5MethodOnes.add(getCaipiaoPl5MethodOne(caipiaoPl5MethodOne, code, 4));
        caipiaoPl5MethodOnes.add(getCaipiaoPl5MethodOne(caipiaoPl5MethodOne, code, 3));
        caipiaoPl5MethodOnes.add(getCaipiaoPl5MethodOne(caipiaoPl5MethodOne, code, 2));
        caipiaoPl5MethodOnes.add(getCaipiaoPl5MethodOne(caipiaoPl5MethodOne, code, 1));
//        caipiaoPl5MethodOneMapper.insertCaipiaoPl5MethodOne(one9);
//        caipiaoPl5MethodOneMapper.insertCaipiaoPl5MethodOne(one8);
//        caipiaoPl5MethodOneMapper.insertCaipiaoPl5MethodOne(one7);
//        caipiaoPl5MethodOneMapper.insertCaipiaoPl5MethodOne(one6);
//        caipiaoPl5MethodOneMapper.insertCaipiaoPl5MethodOne(one5);
//        caipiaoPl5MethodOneMapper.insertCaipiaoPl5MethodOne(one4);
//        caipiaoPl5MethodOneMapper.insertCaipiaoPl5MethodOne(one3);
//        caipiaoPl5MethodOneMapper.insertCaipiaoPl5MethodOne(one2);
//        caipiaoPl5MethodOneMapper.insertCaipiaoPl5MethodOne(one1);

//        MyFileUtil.writeContentToFile("D:\\" + DateUtil.format(caipiaoPl5MethodOne.getQs(), "yyyyMMdd") + ".txt",
//                String.join(",", Pl3NumUtil.positionNum2Code(one3.getPosition1(), one3.getPosition2(), one3.getPosition3())) +"\n", true);
    }

    private static CaipiaoPl5MethodOne getCaipiaoPl5MethodOne(CaipiaoPl5MethodOne caipiaoPl5MethodOne, List<String> code, int ct) {
        CaipiaoPl5MethodOne one = new CaipiaoPl5MethodOne();
        one.setId(IdUtil.getSnowflakeNextId());
        one.setQs(caipiaoPl5MethodOne.getQs());
        one.setCreateTime(DateUtils.getNowDate());
        one.setNumSize(ct);
        Set<String> p1 = new TreeSet<>();
        Set<String> p2 = new TreeSet<>();
        Set<String> p3 = new TreeSet<>();
        Set<String> p4 = new TreeSet<>();
        Set<String> p5 = new TreeSet<>();
        for (String c : code) {
            if (p1.size() < ct) {
                p1.add(c.substring(0, 1));
            }
            if (p2.size() < ct) {
                p2.add(c.substring(1, 2));
            }
            if (p3.size() < ct) {
                p3.add(c.substring(2, 3));
            }
            if (p4.size() < ct) {
                p4.add(c.substring(3, 4));
            }
            if (p5.size() < ct) {
                p5.add(c.substring(4, 5));
            }

            if (p1.size() == ct && p2.size() == ct && p3.size() == ct && p4.size() == ct && p5.size() == ct) {
                break;
            }
        }
        one.setPosition1(String.join(",", p1));
        one.setPosition2(String.join(",", p2));
        one.setPosition3(String.join(",", p3));
        one.setPosition4(String.join(",", p4));
        one.setPosition5(String.join(",", p5));
        return one;
    }
}
